package com.coderworm.webscoketdemo.listener;

import com.coderworm.webscoketdemo.service.WebSocketSessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;

@Component
public class RedisMessageListener implements MessageListener {

    @Autowired
    private WebSocketSessionService webSocketSessionService;

    @Override
    public void onMessage(Message message, byte[] pattern) {
        String channel = new String(message.getChannel());
        String body = new String(message.getBody());

        System.out.println("Received Redis message on " + channel + ": " + body);

        // 广播给所有连接的 WebSocket 用户
        webSocketSessionService.broadcast("Redis event: " + body);
    }
}
